package  
{
	import org.flixel.FlxG;
	import org.flixel.FlxPoint;
	import org.flixel.FlxRect;
	import org.flixel.plugin.photonstorm.BaseTypes.Bullet;
	import org.flixel.plugin.photonstorm.FlxWeapon;
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author Billy Kaifer
	 */
	public class SpreadShot extends Weapon
	{
		[Embed(source = "../media/blue_bullet.png")]private var blue_bullet:Class;

		public var damage:int = 1;
		public var speed:int = 600;
		private var lastFired:uint = 0;
		private var nextFire:uint = 0;
		private var angleOfSpread:uint = 25;
		private var numBullets:uint = 3;

		public function SpreadShot(num_Bullets:int = 3, spreadAngle:uint = 45, fireRate:uint = 100) 
		{
			super("spread_shot", Registry.player);
			makeImageBullet(100, blue_bullet, Registry.positionOffset.x, Registry.positionOffset.y, false, 16, -1, false, false);
			setBulletSpeed(speed);
			setBulletGravity(0, 0);
			setFireRate(fireRate);
			setBulletBounds(new FlxRect(0, 26, FlxG.width, FlxG.height));
			numBullets = num_Bullets;
			angleOfSpread = spreadAngle;
		}
		
		public function setSpreadAngle(angle:uint):void {
			angleOfSpread = angle;
		}
		
		public function setNumProjectiles(num:uint):void {
			numBullets = num;
		}
		
		override public function fire():Boolean {
			if (getFireRate() > 0 && (getTimer() < nextFire))
			{
				return false;
			}
			
			var launchX:int = Registry.positionOffset.x;
			var launchY:int = Registry.positionOffset.y;
			
			launchX += Registry.player.x;
			launchY += Registry.player.y;
			
			lastFired = getTimer();
			nextFire = getTimer() + getFireRate();
			
			try {
				var increment:uint = angleOfSpread / (numBullets - 1);
				var currAngle:int = angleOfSpread / 2;
				var i:int;
				var tempBullet:Bullet;
				
				if (numBullets > 1) {
					for (i = 0; i < numBullets; i++) {
						
						tempBullet = getFreeBullet();

						tempBullet.fireFromAngle(launchX, launchY, currAngle, getBulletSpeed());
						currAngle -= increment;
					}
					
				}else {
					tempBullet = getFreeBullet();
					tempBullet.fireFromAngle(launchX, launchY, 0, getBulletSpeed());
				}
			} catch (error:Error){
				
			}
			
			bulletsFired += numBullets;
			
			return true;
		}
	}

}